/**
 * Copyright (C) 2012 by team 4 M11CQK02-B (Khoa hoc may tinh 2011 - PTIT)
 * 
 * Advanced Algorithm Report
 */
package com.team4.AdvancedAlgorithm.alg;

/**
 * @author <a href="mailto:ndkhoi168@gmail.com">Nguyen Duc Khoi</a>
 * Jan 9, 2012
 */
public class IPAlgorithm
{
   private int x, y, z, v, m;

   private String netIP;

   private String broadcastIP;

   private String[] hostRange = new String[2];

   public IPAlgorithm()
   {
   }

   public IPAlgorithm(int x, int y, int z, int v, int m)
   {
      this.x = x;
      this.y = y;
      this.z = z;
      this.v = v;
      this.m = m;
   }

   public void process()
   {
      // Step a.1. Convert decimal to binary address
      String binaryStr = Utils.convertToBinary(x) + Utils.convertToBinary(y) + Utils.convertToBinary(z)
            + Utils.convertToBinary(v);
      int binaryArrs[] = new int[32];
      for (int i = 0; i < binaryStr.length(); i++)
      {
         binaryArrs[i] = Integer.parseInt(String.valueOf(binaryStr.charAt(i)));
      }

      // Step a.2. Assign 32-m right bit is 0
      int []tmpBinaryArrs = binaryArrs;
      for (int i = 31; i >= m; i--)
      {
         tmpBinaryArrs[i] = 0;
      }

      // Step a.3. Convert result to decimal
      netIP = Utils.convertToIPDecimal(tmpBinaryArrs);
      
      // b. Calculate broadcast Address
      // b.1 is similar with a.1
      // Step b.2 Assign 32-m right bit is 1
      tmpBinaryArrs = binaryArrs;
      for (int i = 31; i >= m; i--)
      {
         tmpBinaryArrs[i] = 1;
      }
      
      // Step b.3. Convert result to decimal
      broadcastIP = Utils.convertToIPDecimal(tmpBinaryArrs);
      
      // c. Calculate host range
      int[] startBinaryArrs = binaryArrs;
      int[] endBinaryArrs = binaryArrs;
      startBinaryArrs[31] = 1;
      for (int i = 30; i >= m; i--)
      {
         startBinaryArrs[i] = 0;
      }
      hostRange[0] = Utils.convertToIPDecimal(startBinaryArrs);
      
      endBinaryArrs[31] = 0;
      for (int i = 30; i >= m; i--)
      {
         endBinaryArrs[i] = 1;
      }
      hostRange[1] = Utils.convertToIPDecimal(endBinaryArrs);
   }

   public int getX()
   {
      return x;
   }

   public void setX(int x)
   {
      this.x = x;
   }

   public int getY()
   {
      return y;
   }

   public void setY(int y)
   {
      this.y = y;
   }

   public int getZ()
   {
      return z;
   }

   public void setZ(int z)
   {
      this.z = z;
   }

   public int getV()
   {
      return v;
   }

   public void setV(int v)
   {
      this.v = v;
   }

   public int getM()
   {
      return m;
   }

   public void setM(int m)
   {
      this.m = m;
   }

   public String getNetIP()
   {
      return netIP;
   }

   public void setNetIP(String netIP)
   {
      this.netIP = netIP;
   }

   public String getBroadcastIP()
   {
      return broadcastIP;
   }

   public void setBroadcastIP(String broadcastIP)
   {
      this.broadcastIP = broadcastIP;
   }

   public String[] getHostRange()
   {
      return hostRange;
   }

   public void setHostRange(String[] hostRange)
   {
      this.hostRange = hostRange;
   }
}
